#!/usr/bin/perl -w
use strict;
use Getopt::Long;

#88888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Usage

#88888888888888888888888888888888888888888888888888888888888888888888888888888888

my $usage = <<'USAGE';

USAGE:

    uniquify.pl
                [--alpha_sort | --numeric_sort]
				[--help]
				[--uniquify_by_regex "(^\d+)"]
				[--verbose]
	
	removes duplicates, optionally sorting
	Prints to STDOUT
USAGE


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#	Get options

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
# mandatory

# optional parameters
my $help;
my $alpha_sort;
my $numeric_sort;
my $uniquify_by_regex;
my $verbose;
GetOptions(
			'alpha_sort'			=> \$alpha_sort,
			'numeric_sort'			=> \$numeric_sort,
			'uniquify_by_regex=s'	=> \$uniquify_by_regex,
			'help'					=> \$help,
			'verbose'					=> \$verbose);

die $usage if ($help);



	
	# read all lines and remove "\n"
	my @arr = <>;
	chomp @arr;

	# uniquify by the whole line unless specified
	$uniquify_by_regex = '(.*)' unless (defined $uniquify_by_regex);
	
	# reverse so that first occurences take precedence
	@arr = reverse @arr;


	my %hash;
	my ($cnt_no_regex_matches, $cnt_empty);
	for (@arr)
	{
		$cnt_empty++, next if $_ eq '';
		$cnt_no_regex_matches++, next unless m/$uniquify_by_regex/;
		$hash{$1} = $_;
	}
	
	# warn if some items did not match the regex
	print STDERR	"Warning:\n\t",
					$cnt_no_regex_matches,
					"\t items did not match the supplied regular expression.\n"
					if ($cnt_no_regex_matches);
	print STDERR	"Warning:\n\t",
					$cnt_empty,
					"\tempty lines were found.\n"
					if ($cnt_empty);
	if ($numeric_sort)
	{
		print "$hash{$_}\n" for (sort{$a <=> $b} keys %hash);
	}
	elsif ($alpha_sort)
	{
		print "$hash{$_}\n" for (sort keys %hash);
	}
	else
	{
		print "$hash{$_}\n" for (keys %hash);
	}
if ($verbose)
{
	print STDERR "\n", scalar keys %hash, "\tunique elements found.\n\n";
}
